# 多阶段构建Dockerfile
FROM maven:3.9-openjdk-17 AS builder

WORKDIR /app

# 复制pom.xml并下载依赖
COPY pom.xml .
RUN mvn dependency:go-offline -B

# 复制源代码并构建
COPY src ./src
RUN mvn clean package -DskipTests

# 运行时镜像
FROM openjdk:17-jdk-slim

WORKDIR /app

# 安装必要的工具
RUN apt-get update && apt-get install -y \
    curl \
    && rm -rf /var/lib/apt/lists/*

# 创建非root用户
RUN groupadd -r appuser && useradd -r -g appuser appuser

# 复制构建的JAR文件
COPY --from=builder /app/target/gcp-proxy-1.0.0.jar app.jar

# 创建日志目录
RUN mkdir -p /logs/gcp-proxy && chown -R appuser:appuser /logs

# 切换到非root用户
USER appuser

# 暴露端口
EXPOSE 5019

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:5019/health || exit 1

# 启动应用
CMD ["java", "-jar", "app.jar"]